require 'rexml/document'
include REXML

class SearchRequest
  
  attr_accessor :query_images, :collection_id, :skin_color_weight, :hair_weight, :recognition_mode, :profile_code, :use_cache, :algorithm

  def initialize(collectionId, query_roi = nil, skin_color_weight = nil, hair_weight = nil, recognition_mode = false, use_cache = false)

    @collection_id = collectionId
    @skin_color_weight = skin_color_weight
    @hair_weight = @hair_weight
    @recognition_mode = recognition_mode
    @use_cache = use_cache
    if query_roi != nil
      @query_images = [QueryImage.new(query_roi, 1)]
    end
    
  end
  
  def roi_ids
    
    rois = []
    
    i = 0;
    
    @query_images.each do |image|
      rois[i] = image.roi_id
      i = i + 1
    end
    
    return rois
    
  end
  
def toXml()
    
    doc = Document.new()
    doc.add_element("search-request")
    
    @query_images.each do |image|
      node = Element.new("query-image")
      
      roiNode = Element.new("roi-id")
      roiNode.text = image.roi_id
      node.elements << roiNode
      
      roiNode = Element.new("weight")
      roiNode.text = image.weight.to_s()
      node.elements << roiNode
      
      doc.root.elements << node
    end
    
    node = Element.new("collection-id")
    node.text = @collection_id
    doc.root.elements << node
    
    node = Element.new("skin-color-weight")
    node.text = @skin_color_weight.to_s()
    doc.root.elements << node
    
    node = Element.new("hair-weight")
    node.text = @hair_weight.to_s()
    doc.root.elements << node
 
    node = Element.new("recognition-mode")
    node.text = @recognition_mode.to_s()
    doc.root.elements << node 
    
    if (@profile_code != nil)
      node = Element.new("profile_code")
      node.text = @profile_code
      doc.root.elements << node
    end    

    node = Element.new("use-cache")
    node.text = @use_cache.to_s()
    doc.root.elements << node     

    
    return doc.to_s()
    
  end

  class QueryImage
    
    attr_accessor :roi_id, :weight
    
    def initialize(roi_id, weight = 1)
      
      @roi_id = roi_id
      @weight = weight
      
    end

  end
  
end